<?php
namespace Admin\Controller;
use Think\Controller;
class RecordController extends Controller {

	public function _initialize()
    {
        //未登录、登录超时、长时间未动
    	if (empty($_SESSION['adminid']) || time() > $_SESSION['adminovertime'] || empty($_SESSION['adminovertime']) ) {

    		$this->redirect('/admin.php/Common/login');
    	}

        session('adminovertime',(time()+60*60*C('adminovertime')));

    }


    /**
     * [task 任务积分报表]
     * @return [type] [description]
     */
    public function task(){

    	$m_user = M('User');
    	$m_task = M('Task');

    	if (IS_POST) {
    		$account = $_REQUEST['account'];
    		$where['account'] = array('like','%'.$account.'%');
    		$useridArr = $m_user->where($where)->order('id asc')->field('id')->select();
    		$user_ids = array();
    		foreach ($useridArr as $key => $value) {
    			$user_ids[] = $value['id'];
    		}

    		$str_userids = implode(',', $user_ids);

    		$where_task['user_id'] = array('in',$str_userids);
    		$parameter = array('user_id'=>$str_userids);

    		$count = $m_task->where($where_task)->count();
	        $Page  = new \Think\Page($count,20,$parameter);
		    $show  = $Page->adminshow();
    	}elseif (IS_GET) {
    		if (!empty($_REQUEST['user_id'])) {
    			$where_task['user_id'] = array('in',$_REQUEST['user_id']);
	        	$parameter = array('user_id'=>$_REQUEST['user_id']);
    		}
    		$count = $m_task->where($where_task)->count();
	        $Page  = new \Think\Page($count,20,$parameter);
		    $show  = $Page->adminshow();
    	
    	}
    	

        $taskList = $m_task->where($where_task)->order('id desc')->limit($Page->firstRow.','.$Page->listRows)->select();
        foreach ($taskList as $key => $value) {
        	$where_user['id'] = $value['user_id'];
        	$userAccount = $m_user->where($where_user)->getfield('account');
        	$taskList[$key]['account'] = $userAccount;
        }

        $this->assign('page',$show);
        $this->assign('taskList',$taskList);
    	$this->display();
    }



    /**
     * [center 中心积分报表]
     * @return [type] [description]
     */
    public function center(){

    	$m_user = M('User');
    	$m_center = M('Center');

    	if (IS_POST) {
    		$account = $_REQUEST['account'];
    		$where['account'] = array('like','%'.$account.'%');
    		$useridArr = $m_user->where($where)->order('id asc')->field('id')->select();
    		$user_ids = array();
    		foreach ($useridArr as $key => $value) {
    			$user_ids[] = $value['id'];
    		}

    		$str_userids = implode(',', $user_ids);

    		$where_center['user_id'] = array('in',$str_userids);
    		$parameter = array('user_id'=>$str_userids);

    		$count = $m_center->where($where_center)->count();
	        $Page  = new \Think\Page($count,20,$parameter);
		    $show  = $Page->adminshow();
    	}elseif (IS_GET) {
    		if (!empty($_REQUEST['user_id'])) {
    			$where_center['user_id'] = array('in',$_REQUEST['user_id']);
	        	$parameter = array('user_id'=>$_REQUEST['user_id']);
    		}
    		$count = $m_center->where($where_center)->count();
	        $Page  = new \Think\Page($count,20,$parameter);
		    $show  = $Page->adminshow();
    	
    	}
    	

        $centerList = $m_center->where($where_center)->order('id desc')->limit($Page->firstRow.','.$Page->listRows)->select();
        foreach ($centerList as $key => $value) {
        	$where_user['id'] = $value['user_id'];
        	$userAccount = $m_user->where($where_user)->getfield('account');
        	$centerList[$key]['account'] = $userAccount;
        }

        $this->assign('page',$show);
        $this->assign('centerList',$centerList);
    	$this->display();
    }


    /**
     * [trading 交易积分报表]
     * @return [type] [description]
     */
    public function trading(){

    	$m_user = M('User');
    	$m_trading = M('Trading');

    	if (IS_POST) {
    		$account = $_REQUEST['account'];
    		$where['account'] = array('like','%'.$account.'%');
    		$useridArr = $m_user->where($where)->order('id asc')->field('id')->select();
    		$user_ids = array();
    		foreach ($useridArr as $key => $value) {
    			$user_ids[] = $value['id'];
    		}

    		$str_userids = implode(',', $user_ids);

    		$where_trading['user_id'] = array('in',$str_userids);
    		$parameter = array('user_id'=>$str_userids);

    		$count = $m_trading->where($where_trading)->count();
	        $Page  = new \Think\Page($count,20,$parameter);
		    $show  = $Page->adminshow();
    	}elseif (IS_GET) {
    		if (!empty($_REQUEST['user_id'])) {
    			$where_trading['user_id'] = array('in',$_REQUEST['user_id']);
	        	$parameter = array('user_id'=>$_REQUEST['user_id']);
    		}
    		$count = $m_trading->where($where_trading)->count();
	        $Page  = new \Think\Page($count,20,$parameter);
		    $show  = $Page->adminshow();
    	
    	}
    	

        $tradingList = $m_trading->where($where_trading)->order('id desc')->limit($Page->firstRow.','.$Page->listRows)->select();
        foreach ($tradingList as $key => $value) {
        	$where_user['id'] = $value['user_id'];
        	$userAccount = $m_user->where($where_user)->getfield('account');
        	$tradingList[$key]['account'] = $userAccount;
        }

        $this->assign('page',$show);
        $this->assign('tradingList',$tradingList);
    	$this->display();
    }


    /**
     * [keydynamic 动态秘钥报表]
     * @return [type] [description]
     */
    public function keydynamic(){

    	$m_user = M('User');
    	$m_keydynamic = M('Key_dynamic');

    	if (IS_POST) {
    		$account = $_REQUEST['account'];
    		$where['account'] = array('like','%'.$account.'%');
    		$useridArr = $m_user->where($where)->order('id asc')->field('id')->select();
    		$user_ids = array();
    		foreach ($useridArr as $key => $value) {
    			$user_ids[] = $value['id'];
    		}

    		$str_userids = implode(',', $user_ids);

    		$where_keydynamic['user_id'] = array('in',$str_userids);
    		$parameter = array('user_id'=>$str_userids);

    		$count = $m_keydynamic->where($where_keydynamic)->count();
	        $Page  = new \Think\Page($count,20,$parameter);
		    $show  = $Page->adminshow();
    	}elseif (IS_GET) {
    		if (!empty($_REQUEST['user_id'])) {
    			$where_keydynamic['user_id'] = array('in',$_REQUEST['user_id']);
	        	$parameter = array('user_id'=>$_REQUEST['user_id']);
    		}
    		$count = $m_keydynamic->where($where_keydynamic)->count();
	        $Page  = new \Think\Page($count,20,$parameter);
		    $show  = $Page->adminshow();
    	
    	}
    	

        $keydynamicList = $m_keydynamic->where($where_keydynamic)->order('id desc')->limit($Page->firstRow.','.$Page->listRows)->select();
        foreach ($keydynamicList as $key => $value) {
        	$where_user['id'] = $value['user_id'];
        	$userAccount = $m_user->where($where_user)->getfield('account');
        	$keydynamicList[$key]['account'] = $userAccount;
        }

        $this->assign('page',$show);
        $this->assign('keydynamicList',$keydynamicList);
    	$this->display();
    }



    /**
     * [keytask 动态秘钥报表]
     * @return [type] [description]
     */
    public function keytask(){

    	$m_user = M('User');
    	$m_keytask = M('Key_task');

    	if (IS_POST) {
    		$account = $_REQUEST['account'];
    		$where['account'] = array('like','%'.$account.'%');
    		$useridArr = $m_user->where($where)->order('id asc')->field('id')->select();
    		$user_ids = array();
    		foreach ($useridArr as $key => $value) {
    			$user_ids[] = $value['id'];
    		}

    		$str_userids = implode(',', $user_ids);

    		$where_keytask['user_id'] = array('in',$str_userids);
    		$parameter = array('user_id'=>$str_userids);

    		$count = $m_keytask->where($where_keytask)->count();
	        $Page  = new \Think\Page($count,20,$parameter);
		    $show  = $Page->adminshow();
    	}elseif (IS_GET) {
    		if (!empty($_REQUEST['user_id'])) {
    			$where_keytask['user_id'] = array('in',$_REQUEST['user_id']);
	        	$parameter = array('user_id'=>$_REQUEST['user_id']);
    		}
    		$count = $m_keytask->where($where_keytask)->count();
	        $Page  = new \Think\Page($count,20,$parameter);
		    $show  = $Page->adminshow();
    	
    	}
    	

        $keytaskList = $m_keytask->where($where_keytask)->order('id desc')->limit($Page->firstRow.','.$Page->listRows)->select();
        foreach ($keytaskList as $key => $value) {
        	$where_user['id'] = $value['user_id'];
        	$userAccount = $m_user->where($where_user)->getfield('account');
        	$keytaskList[$key]['account'] = $userAccount;
        }

        $this->assign('page',$show);
        $this->assign('keytaskList',$keytaskList);
    	$this->display();
    }



}